#!/bin/sh
#
# Copyright (c) Nuvoton Technology Corp. All rights reserved.
#
# Read/Write server parameters

eval `/mnt/ESensor/htdocs/proccgi.sh $*`

cfgServerConfPath="/mnt/ESensor/etc/msloader.conf"

NetlinkPulgPath=/mnt/nand1-1/netlink

echo "Content-type: application/json"
echo ""


varTIME_ZONE=`awk -F"[=#]" '$1 == "Time_Zone"{ print $2}' $cfgServerConfPath`



if [ "$FORM_doAction" == "read" ] ; then

#检测业务程序是否正常启动
ps_out=`ps -ef | grep "wap800_app" | grep -v 'grep' | grep -v $0`
result=$(echo $ps_out | grep "wap800_app")
if [[ "$result" != "" ]];then
	varSystemStatus=`cat /tmp/system_info | grep "SYSSTA ." | awk '{print $2} '`
else
	varSystemStatus="异常";
fi

#检测LAN网卡
rLANSTA=`ifconfig eth0 | grep RUNNING -o`;
if [ "$rLANSTA" == "" ]; then
	rLANSTA="断开"
	varLANINET="0.0.0.0";
	varLANBCAST="0.0.0.0";
	varLANMASK="0.0.0.0";
else
	rLANSTA="运行"
	varLANINET=`ifconfig eth0 | grep "inet addr:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o | grep "[0-9.]\+" -o`;
	varLANBCAST=`ifconfig eth0 | grep "Bcast:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o | grep "[0-9.]\+" -o`;
	varLANMASK=`ifconfig eth0 | grep "Mask:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o | grep "[0-9.]\+" -o`;
fi


#检测WIFI网卡
varWLANSTA=`ifconfig wlan0 | grep RUNNING -o`;
if [ "$varWLANSTA" == "" ]; then
	varWLANSTA="断开"
	varWLAN0INET="0.0.0.0";
	varWLAN0BCAST="0.0.0.0";
	varWLAN0MASK="0.0.0.0";
else
	varWLANSTA="运行";
	varWLAN0INET=`ifconfig wlan0 | grep "inet addr:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o | grep "[0-9.]\+" -o`;
	varWLAN0BCAST=`ifconfig wlan0 | grep "Bcast:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o | grep "[0-9.]\+" -o`;
	varWLAN0MASK=`ifconfig wlan0 | grep "Mask:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o | grep "[0-9.]\+" -o`;
fi

#获取NRF的通道
	varNRFCH=`cat /tmp/system_info | grep "NRFCH ." | grep "[0-9]\+" -o`

#获取时间
	varDATE=`date`
	

cat << EOF
	{
		"STATUS": "$varSystemStatus",
		"DATE": "$varDATE",
		"LANSTA": "$rLANSTA",
		"WLANSTA": "$varWLANSTA",
		"NRFCH": "$varNRFCH",
		"LANINET": "$varLANINET",
		"LANBCAST": "$varLANBCAST",
		"LANMASK": "$varLANMASK",
		"WLAN0INET": "$varWLAN0INET",
		"WLAN0BCAST": "$varWLAN0BCAST",
		"WLAN0MASK": "$varWLAN0MASK"
	}
EOF

fi

if [ "$FORM_doAction" == "reset" ] ; then
rm /mnt/nand1-2/etc/network_config
sed "1c admin:admin:0" /mnt/nand1-2/etc/.ESensor.user -i
reboot
cat << EOF
	{
		"MSG": "Server date/time is set as $varDATE"
	}
EOF
	
fi

if [ "$FORM_doAction" == "reboot" ] ; then
reboot
fi

if [ "$FORM_doAction" == "forget" ] ; then
cp /mnt/nand1-2/etc/.ESensor.user /mnt/nand1-2/etc/.ESensorOld.user
sed "1c admin:admin:testdemo:0" /mnt/nand1-2/etc/.ESensor.user -i
sleep 10
cp /mnt/nand1-2/etc/.ESensorOld.user /mnt/nand1-2/etc/.ESensor.user
fi
